﻿namespace TestTool.Tests.Common.TestBase
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.ServiceModel;
    using System.Text;

    public static class Extensions
    {
        public static bool IsValidOnvifFault(this FaultException ex, string faultCodes)
        {
            string str;
            return ex.IsValidOnvifFault(faultCodes, out str);
        }

        public static bool IsValidOnvifFault(this FaultException ex, string faultCodes, out string verificationDump)
        {
            if (ex == null)
            {
                verificationDump = "fault exception is null";
                return false;
            }
            string str = "http://www.onvif.org/ver10/error";
            string str2 = "http://www.w3.org/2003/05/soap-envelope";
            string[] strArray = faultCodes.Split(new char[] { '/' });
            if (ex.Code == null)
            {
                verificationDump = "no fault code";
                return false;
            }
            if (ex.Code.Namespace.ToLower() != str2)
            {
                verificationDump = string.Format("fault code has incorrect namespace ({0})", ex.Code.Namespace);
                return false;
            }
            if (ex.Code.Name.Contains("/"))
            {
                verificationDump = string.Format("code {0} is incorrect", ex.Code.Name);
                return false;
            }
            StringBuilder builder = new StringBuilder(string.Format("env:{0}", ex.Code.Name));
            for (FaultCode code = ex.Code.SubCode; code != null; code = code.SubCode)
            {
                if (code.Namespace.ToLower() != str)
                {
                    verificationDump = string.Format("subcode {0} has incorrect namespace ({1})", code.Name, code.Namespace);
                    return false;
                }
                if (code.Name.Contains("/"))
                {
                    verificationDump = string.Format("subcode {0} is incorrect", code.Name);
                    return false;
                }
                builder.AppendFormat("/ter:{0}", code.Name);
            }
            if (strArray.Length != 0)
            {
                StringBuilder builder2 = new StringBuilder(string.Format("env:{0}", strArray[0]));
                for (int i = 1; i < strArray.Length; i++)
                {
                    builder2.AppendFormat("/ter:{0}", strArray[i]);
                }
                string str3 = builder.ToString();
                string str4 = builder2.ToString();
                if (str3 != str4)
                {
                    verificationDump = string.Format("fault subcodes sequence is incorrect. Expected: {0}, actual: {1}", str4, str3);
                    return false;
                }
            }
            verificationDump = string.Empty;
            return true;
        }
    }
}

